package es.android.lottoscan;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class MainActivity extends Activity implements LottoCheckListener {
	public static final String TAG = "MainActivity";
	public static final int MESSAGE_ERROR = 21;
	public static final int MESSAGE_OK = 2;
	public LottoData data = new LottoData();
	
	LottoView lottoView;
	TextView statusView;
	LottoCheck lottoCheck;
	String prize;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		requestWindowFeature(Window.FEATURE_PROGRESS);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		setContentView(R.layout.main);
		
        lottoView = (LottoView) findViewById(R.id.MyLottoView);
        statusView = (TextView) findViewById(R.id.Status);
        
        lottoCheck = new LottoCheck();
    }

    @Override
	protected void onResume() {
        
        if (lottoView != null) lottoView.setData(data);
        
        Log.d(TAG, "lottoView = " + lottoView);

		super.onResume();
	}
    
    /**
	 * Called to update
	 */
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			update(msg);
		}
	};
	
	void update(Message msg) {

		switch (msg.what) {
		case MESSAGE_ERROR:
			statusView.setTextColor(Color.BLACK);
        	statusView.setText(R.string.error);
			break;
		
		case MESSAGE_OK:
			if (prize == null) {
				statusView.setTextColor(Color.BLACK);
	        	statusView.setText(R.string.no_prize);				
			} else {
				statusView.setTextColor(Color.RED);
	        	statusView.setText(getString(R.string.prize) + " " + prize);
			}
			break;
		}
	}

	public void onTestAction(View v) {
    	data = new LottoData();
        //scanned("51029051500316438590");
    	scanned("50900051500063668590");
    	//scanned("50900051500163998590");
    	//scanned("50010051500163998590");
    };
    
    public void onQuitAction(View v) {
        this.finish();
    }
    public void onScanAction(View v) {
        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        //intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
        try {
        	startActivityForResult(intent, 0);
        } catch (Exception e) {
    		AlertDialog.Builder builder = new AlertDialog.Builder(this);
    		builder.setCancelable(false);
    		
    		builder.setPositiveButton(R.string.dialog_yes, new OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://market.android.com/details?id=com.google.zxing.client.android"));
					startActivity(intent);					
				}
			});		
    		builder.setTitle(R.string.dialog_install_scanner_title);
    		builder.setMessage(R.string.dialog_install_scanner);
    		builder.create().show();
        }
    };
    
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                String contents = intent.getStringExtra("SCAN_RESULT");
                //String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                // Handle successful scan
                scanned(contents);
                
            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
            }
        }
    }

    private void scanned(String contents) {
    	data.parseNumberString(contents);
    	if (lottoView != null) lottoView.setData(data);
		statusView.setTextColor(Color.BLACK);
    	statusView.setText(R.string.query);
    	lottoCheck.query(data, this);
    }
    

	public void checkOk(String prize) {
		this.prize = prize;
		handler.sendEmptyMessage(MESSAGE_OK);
	}

	public void checkError() {
		handler.sendEmptyMessage(MESSAGE_ERROR);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}